package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyMapper strategyMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId()==null) {
            strategyMapper.insert(strategy);
        }else{
            strategyMapper.updateByPrimaryKey(strategy);
        }
    }

    @Override
    public List<Strategy> list() {
        return strategyMapper.selectAll();
    }

    @Override
    public PageInfo selectCommend(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectCommend(qo);
        return new PageInfo(list);
    }

    @Override
    public Strategy selectByStrategyId(Long id) {

        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo selectByRegionId(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectCommend(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Map<String, Object>> selectStrategyByKeyword(String keyword) {

        return strategyMapper.selectStrategyByKeyword(keyword);
    }

    @Override
    public void updateViewCount(Long strategyId) {
        strategyMapper.updateViewCount(strategyId);
    }


}
